package com.btpj.hospital_server.service;

import com.btpj.hospital_server.dto.HospitalDoctorDTO;
import com.btpj.hospital_server.dto.param.HospitalDoctorParam;
import com.btpj.hospital_server.entity.HospitalDoctor;
import com.btpj.hospital_server.mapper.HospitalDoctorMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 医生信息Service
 *
 * @author BTPJ  2020/8/27
 */
@Service
public class HospitalDoctorService {

    @Resource
    HospitalDoctorMapper hospitalDoctorMapper;

    @Resource
    HospitalSpecialService hospitalSpecialService;

    @Resource
    HospitalOutpatientService hospitalOutpatientService;

    /**
     * 添加医生信息
     *
     * @param param HospitalDoctorParam
     * @return 是否添加成功
     */
    public boolean addDoctor(HospitalDoctorParam param) {
        HospitalDoctor doctor = new HospitalDoctor();
        BeanUtils.copyProperties(param, doctor);
        doctor.setGmtCreate(new Date());
        doctor.setGmtModified(new Date());
        return hospitalDoctorMapper.insertSelective(doctor) > 0;
    }

    /**
     * 是否存在指定编号的医生
     *
     * @param id 医生编号
     * @return 是否存在指定编号的医生
     */
    public boolean existDoctor(Long id) {
        return hospitalDoctorMapper.selectByPrimaryKey(id) != null;
    }

    /**
     * 更新医生信息
     *
     * @param id    医生编号
     * @param param HospitalDoctorParam
     * @return 是否更新成功
     */
    public boolean updateDoctor(Long id, HospitalDoctorParam param) {
        HospitalDoctor doctor = hospitalDoctorMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(param, doctor);
        doctor.setGmtModified(new Date());
        return hospitalDoctorMapper.updateByPrimaryKeySelective(doctor) > 0;
    }

    /**
     * 删除医生信息
     *
     * @param id 医生编号
     * @return 是否删除成功
     */
    public boolean deleteDoctor(Long id) {
        return hospitalDoctorMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 获取医生信息
     *
     * @param id 医生编号
     * @return 是否删除成功
     */
    public HospitalDoctorDTO getDoctorDetail(Long id) {
        HospitalDoctor doctor = hospitalDoctorMapper.selectByPrimaryKey(id);
        return convert(doctor);
    }

    /**
     * 获取医生信息列表
     *
     * @param name         筛选的医生名
     * @param specialId    筛选的专科编号
     * @param outpatientId 筛选的门诊编号
     * @return 医生信息列表
     */
    public List<HospitalDoctorDTO> getList(String name, Long specialId, Long outpatientId) {
        List<HospitalDoctor> hospitalDoctors = hospitalDoctorMapper.selectByNameOrSpecialIdOrOutpatientId(name, specialId, outpatientId);
        List<HospitalDoctorDTO> doctorDTOS = new ArrayList<>();
        for (HospitalDoctor hospitalDoctor : hospitalDoctors) {
            doctorDTOS.add(convert(hospitalDoctor));
        }
        return doctorDTOS;
    }

    /**
     * 将HospitalDoctor封装转化成HospitalDoctorDTO
     *
     * @param hospitalDoctor HospitalDoctor
     * @return HospitalDoctorDTO
     */
    private HospitalDoctorDTO convert(HospitalDoctor hospitalDoctor) {
        if (hospitalDoctor == null) {
            return null;
        }
        HospitalDoctorDTO doctorDTO = new HospitalDoctorDTO();
        BeanUtils.copyProperties(hospitalDoctor, doctorDTO);
        doctorDTO.setSpecialName(hospitalSpecialService.getName(hospitalDoctor.getSpecialId()));
        doctorDTO.setOutpatientName(hospitalOutpatientService.getName(hospitalDoctor.getOutpatientId()));
        return doctorDTO;
    }
}
